package com.hxzy.projectssmplus.security;

import com.hxzy.projectssmplus.entity.SysAdminUser;
import com.hxzy.projectssmplus.exception.ServiceException;
import com.hxzy.projectssmplus.service.SysAdminUserService;
import com.hxzy.projectssmplus.service.SysMenuService;
import com.hxzy.projectssmplus.service.SysRoleService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Log4j2
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    /**
     * 角色业务逻辑
     */
    @Autowired
    private SysRoleService  sysRoleService;

    /**
     * 菜单业务逻辑
     */
    @Autowired
    private SysMenuService  sysMenuService;

    /**
     * 用户业务逻辑
     */
    @Autowired
    private SysAdminUserService  sysAdminUserService;

    /**
     * 根据用户名登录操作
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysAdminUser adminUser = this.sysAdminUserService.findUserByUserName(username);
        if(adminUser==null){
            log.info("登录用户：{} 不存在.", username);
            //抛出异常
            throw new ServiceException("用户名或密码错误");
        }
        //状态
        if(adminUser.getLocked().equals(1)){
            log.info("登录用户：{} 被锁定.", username);
            //抛出异常
            throw new ServiceException("该账户被锁定无法登录");
        }

        //返回 UserDetails 接口 实现 (设定用户名，密码????在哪里判断[spring security配置文件中判断]， 权限)

        return createLoginUser(adminUser);
    }

    //UserDetails 接口 实现 (设定用户名，密码????在哪里判断[spring security配置文件中判断]， 权限)
    // jwt + uuid 连接redis操作 (生成令牌)
    public UserDetails createLoginUser(SysAdminUser adminUser ){

        Set<SimpleGrantedAuthority> grantedAuthoritySet=new HashSet<>();
       //用户id查询它拥菜单和角色权限
        Set<String> ownerRoleSet=this.sysRoleService.ownerRoleByUserId(adminUser.getAdminUserId());
        if(ownerRoleSet!=null && ownerRoleSet.size()>0){
            for(String role : ownerRoleSet){
                grantedAuthoritySet.add( new SimpleGrantedAuthority("ROLE_"+role));
            }

        }

        //菜单权限
        Set<String> owernMenuSet=this.sysMenuService.ownerMenuByUserId(adminUser.getAdminUserId());
        if(owernMenuSet!=null && owernMenuSet.size()>0){
            for(String menu : owernMenuSet){
                grantedAuthoritySet.add( new SimpleGrantedAuthority(menu));
            }
        }

        UserDetails  userDetails=new LoginUserDetails(adminUser.getAdminUserId(), adminUser,grantedAuthoritySet);
        return userDetails;

    }
}
